package com.zxcl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxcl.dto.PageDTO;
import com.zxcl.dto.SysRoleQueryDTO;
import com.zxcl.utils.AuthUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxcl.domain.SysRole;
import com.zxcl.mapper.SysRoleMapper;
import com.zxcl.service.SysRoleService;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{

    /**
     * 查询所有角色
     * 如果是超级店铺的员工，可以查询出所有角色，如果不是，只查非管理员的角色
     */
    @Override
    public List<SysRole> loadSysRoleList() {

        Long shopId = AuthUtil.getShopId();
        List<SysRole> list = this.getBaseMapper().selectList(null);
        //如果shopId不是1 就过滤掉超级管理员
        if(!shopId.equals(1L)){
            list=list.stream().filter(sysRole -> !sysRole.getRoleId().equals(1L)).collect(Collectors.toList());
        }
        return list;
    }


    @Override
    public Page<SysRole> loadSysRolePage(PageDTO pageDTO, SysRoleQueryDTO sysRoleQueryDTO) {
        Page<SysRole> page = new Page<>(pageDTO.getCurrent(), pageDTO.getSize());
        return  getBaseMapper().selectPage(page,new LambdaQueryWrapper<SysRole>()
                .like(StringUtils.hasText(sysRoleQueryDTO.getRoleName()),SysRole::getRoleName,sysRoleQueryDTO.getRoleName()));
    }
}
